﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MuaDeal
{
    public partial class DangNhap : Form
    {
        public DangNhap()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void lbdangnhap_Click(object sender, EventArgs e)
        {

        }

        private void btdangnhap_Click(object sender, EventArgs e)
        {
            //Datahelper helper = new Datahelper();
            //int kq=helper.ThietLapKetNoi();
            //if(kq==1)
            //{
            //    MessageBox.Show("Kết nối CSDL thành công");
            //}
            //else
            //{
            //    MessageBox.Show("Kết nối CSDL không thành công");
            //}
            int kq = 0;
            try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_DangNhap";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1= comman.Parameters.Add("@tendn", SqlDbType.Char,30);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = txttentaikhoan.Text;

                SqlParameter bien2 = comman.Parameters.Add("@matkhau", SqlDbType.Char, 30);
                bien2.Direction = ParameterDirection.Input;
                bien2.Value = textBox1.Text;

                comman.Connection = helper.Connection;
                SqlDataReader read = comman.ExecuteReader();
                if (read.Read())
                {
              
                    kq = int.Parse(read[2].ToString());
                }
                else
                {
                    MessageBox.Show("Đăng nhập không hợp lệ");
                }
                helper.Close();
            }
            catch (SqlException ex) { MessageBox.Show("Kết nối bị lỗi "+ex.Message); }
            if (kq == 1)
            {
                MainAD frm = new MainAD();
                this.Visible = false;
                frm.ShowDialog();
                this.Close();
            }
            if(kq==2)
            {
                MainNV frm = new MainNV();
                this.Visible = false;
                frm.ShowDialog();
                this.Close();
            }
            if (kq==3)
            {
                MainKH frm = new MainKH();
                frm.Tenkh = txttentaikhoan.Text;
                this.Visible = false;
                frm.ShowDialog();
                this.Close();
            }
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}